package com.penta.config;

import java.io.*;
import java.util.Properties;

public class ConfigLoader {

    private Properties properties = new Properties();
    private String propertyFilePath;

    public ConfigLoader(String propertyFilePath) {
        this.propertyFilePath = propertyFilePath;
        loadProperties();
    }

    private void loadProperties() {
        try {
            InputStream input = ConfigLoader.class.getClassLoader().getResourceAsStream(propertyFilePath);
            // 加载配置文件
            properties.load(input);
        } catch (IOException ex) {
            System.out.println("读取系统配置文件出错");
            ex.printStackTrace();
        }
    }

    /**
     * 读取配置
     * @param key
     * @return
     */
    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    public void setProperty(String key, String value) {
        properties.setProperty(key, value);
        try (FileOutputStream output = new FileOutputStream(propertyFilePath)) {
            // 保存修改后的配置
            properties.store(output, null);
        } catch (IOException io) {
            io.printStackTrace();
        }
    }
}
